package my.poemhelper.dao;

import my.poemhelper.vo.Idiom;
import my.poemhelper.vo.PageControl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Component;

@Component
public class IdiomDaoImpl extends BaseDao implements IdiomDao {


	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public void insertIdiom(Idiom idiom) {
		String sql = "INSERT INTO idiom (idiomword,idiominfo,inserttime) " +
				"values('"+idiom.getIdiomword()+"','"+idiom.getIdiominfo()+"','"+ this.getNowStr() +"')";
		jdbcTemplate.execute(sql);
	}

	@Override
	public void queryIdiom(String idiom, PageControl<Idiom> pageControl) {
		
		String coutSql = "select count(1) from idiom";
		int count = jdbcTemplate.queryForInt(coutSql);
		pageControl.getData().clear();
		pageControl.setTotalNum(count);
		pageControl.setTotalPage(count/pageControl.getPageSize()+(count%pageControl.getPageSize()==0?0:1));
		
		StringBuilder sql = new StringBuilder();
		if(idiom==null || "".equals(idiom)){
			sql.append("select * from idiom");
		}else{
			sql.append("select * from idiom where idiomword='").append(idiom).append("'");
		}
		sql.append(" order by inserttime");
		sql.append(" limit ").append((pageControl.getCurrentPage()-1)*pageControl.getPageSize()).append(",").append(pageControl.getPageSize());
		
		SqlRowSet rs = jdbcTemplate.queryForRowSet(sql.toString());
		
		
		while(rs.next()){
			Idiom i = new Idiom();
			i.setIdiomword(rs.getString("idiomword"));
			i.setIdiominfo(rs.getString("idiominfo"));
			i.setId(rs.getLong("id"));
			i.setInsertTime(rs.getString("inserttime"));
			pageControl.getData().add(i);
		}
	}

	@Override
	public void delIdiom(long id) {
		// TODO Auto-generated method stub

	}

}
